<?php

namespace App\Models;

use App\Models\BaseModel;

class User extends BaseModel
{
    /**
     * 与模型关联的表名
     * @var string
     */
    protected $table = 'users';

    /**
     * 被隐藏属性。
     * @var array
     */
    protected $hidden = ['password'];

    /**
     * $fillable可以被批量赋值的属性.   $guarded为不能批量赋值
     * @var array
     */
    protected $guarded = ['id'];

    protected $listWith = [];

    protected $disableFilter = ['password'];

    /**
     * 属性类型转.
     * @var array
     */
    protected $casts = [];


    /**
     * 相关
     */
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

/*
    //新建时忽略,自动生成不可由用户更改的字段
    protected $ignoreCreate = ['id', 'created_at', 'updated_at',  'remember_token'];

    //修改时忽略
    protected $ignoreUpdate = [0, 1, 'password', 'password_confirmation', 'created_at', 'updated_at',  'remember_token'];
    //后台index时显示
    protected $gridFields = ['id', 'avatar', 'name', 'username', 'email', 'created_at', 'updated_at'];
    //后台show时显示
    protected $showFields = ['id', 'avatar', 'name', 'username', 'email', 'created_at', 'updated_at'];

    //修改时与新建时设置不一样的，此设置在修改时将替换新建时的设置，如有闭包函数等可直接在addUpdateReplace方法中修改
    protected $updateReplace = [
        'name'  => [
            'display' => '登录名',
        ],
    ];
    //$formSet的增强设置方式，将补充或替换$formSet，以防匿名函数等出错
    public function addFormSet(): array
    {
        return [
            'id'    => [
                'display' => 'ID'
            ],
            'name'  => [
                'text' => '登录名',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,name,{{id}}"],
                'required'    => null
            ],
            'username' => [
                'text' => '呢称',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,username,{{id}}"],
                'required'    => null
            ],
            'email' => [
                'email' => '邮箱',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,email,{{id}}"],
                'required'    => null
            ],
            'phone' => [
                'mobile' => '手机',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,phone,{{id}}"],
                'required'    => null,
                'options' => ['mask' => '999 9999 9999']
            ],
            'avatar' => [
                'image' => '头像',
            ],
            'password' => [
                'password' => '密码',
                'rules'     => 'required|confirmed',
            ],
            'password_confirmation' => [
                'password' => '重复密码',
                'rules'     => 'required'
            ],
            '0'  => [
                'ignore' => ['password_confirmation'],
            ],
            '1'    => [
                'saving' => function ($form) {
                    if ($form->password && $form->model()->password != $form->password) {
                        $form->password = bcrypt($form->password);
                    }
                }
            ],
            'created_at'    => [
                'display' => '注册日期'
            ],
            'updated_at'    => [
                'display' => '活动日期'
            ],
        ];
    }
*/
